package com.reone.imagewatcherpreview.util;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.util.SparseArray;
import android.widget.ImageView;

import com.reone.imagewatcher.iielse.ImageWatcher;
import com.reone.imagewatcher.iielse.ImageWatcherHelper;
import com.reone.imagewatcher.listener.OnPictureLongPressListener;
import com.reone.imagewatcher.listener.OnStateChangedListener;
import com.reone.imagewatcherpreview.App;
import com.reone.imagewatcherpreview.R;

import java.util.List;

/**
 * Created by wangxingsheng on 2019/4/12.
 * desc:
 */
public class ImagePreview {
    private FragmentActivity activity;
    private ImageWatcherHelper iwHelper;

    public ImagePreview(FragmentActivity activity) {
        this.activity = activity;
        buildImageWatcherHelper();
    }

    public void show(ImageView i, SparseArray<ImageView> imageGroupList, List<Uri> urlList) {
        iwHelper.show(i, imageGroupList, urlList);
    }

    private void buildImageWatcherHelper() {
        iwHelper = ImageWatcherHelper.with(activity, new GlideSimpleLoader()) // 一般来讲， ImageWatcher 需要占据全屏的位置
                .setTranslucentStatus(0) // 如果不是透明状态栏，你需要给ImageWatcher标记 一个偏移值，以修正点击ImageView查看的启动动画的Y轴起点的不正确
                .setErrorImageRes(R.mipmap.error_picture) // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API
                .setOnPictureLongPressListener(new OnPictureLongPressListener() {
                    @Override
                    public void onPictureLongPress(ImageView v, Uri uri, int pos) {
                        // 长按图片的回调，你可以显示一个框继续提供一些复制，发送等功能
                        talk("长按了第" + (pos + 1) + "张图片");
                    }
                })
                .addOnStateChangedListener(new OnStateChangedListener() {
                    @Override
                    public void onStateChangeUpdate(ImageWatcher imageWatcher, ImageView clicked, int position, Uri uri, float animatedValue, int actionTag) {
                        Log.e("IW", "onStateChangeUpdate [" + position + "][" + uri + "][" + animatedValue + "][" + actionTag + "]");
                    }

                    @Override
                    public void onStateChanged(ImageWatcher imageWatcher, int position, Uri uri, int actionTag) {
                        if (actionTag == ImageWatcher.STATE_ENTER_DISPLAYING) {
                            talk("点击了图片 [" + position + "]" + uri + "");
                        } else if (actionTag == ImageWatcher.STATE_EXIT_HIDING) {
                            talk("退出了查看大图");
                        }
                    }
                });
    }

    private void talk(final String str) {
        App.talk(str);
    }
}
